package indexingSystem; 
import java.io.IOException;
import java.io.InputStream;

import com.lowagie.text.pdf.*; 
import com.lowagie.text.pdf.parser.PdfTextExtractor;

public class PdfScanner extends InputStream {
	PdfReader reader;
	PdfTextExtractor extractor;
	int numberOfPages;
	int currentPage = -1;
	String pageContent;
	int currentPageLength = -1;
	int currentByteIndex = -1;
	int totalchars=0;
	PdfScanner(String path) throws IOException {
		reader = new PdfReader(path);
		numberOfPages = reader.getNumberOfPages();
		extractor = new PdfTextExtractor(reader);
	}
	@Override
	public int read() throws IOException {
		currentByteIndex++;
		if(currentByteIndex < currentPageLength) {
			return pageContent.codePointAt(currentByteIndex);
		} else {
			currentPage++;
			if (currentPage < numberOfPages) {
				pageContent = extractor.getTextFromPage(currentPage+1);
				currentPageLength = pageContent.length();
				currentByteIndex = -1;
				return read();
			}
		}
		return -1;
	}
	public void close() {
		reader.close();
	}
}
